package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ware.annotation.Token;
import com.example.ware.dao.WareMapper;
import com.example.ware.model.Spage.ShowPage;
import com.example.ware.model.po.Admin;
import com.example.ware.model.po.Place;
import com.example.ware.model.po.Ware;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.model.vo.WareInfo;
import com.example.ware.service.PlaceService;
import com.example.ware.service.WareService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/ware")
public class WareController {

    @Autowired
    WareService wareService;

    @Resource
    WareMapper wareMapper;

    @Autowired
    PlaceService placeService;


    @Resource
    RedisTemplate redisTemplate;

//    获取仓库列表（多选项）
    @GetMapping("/List")
    public ResponseData getList(String name, String location, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
        ShowPage result=wareService.getWareList(name,location,pageNum,pageSize);

        return new ResponseData(result,true,"获取成功",200);

    }




//    初始化仓库信息（创建仓库）
    @Token
    @PostMapping("/init")
    public ResponseData initWare(@RequestBody Ware ware){
        Date data=new Date();
        ware.setCreatetime(data);

        ware.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        boolean i= wareService.save(ware);

        if(i){
            return new ResponseData(i,true,"创建成功",200);
        }else{
            return new ResponseData(i,false,"创建失败",201);
        }
    }


//    获取所有仓库的名字
    @GetMapping("/all")
    public ResponseData all(){
        List<Ware> wares=wareService.list();
        if(wares!=null){
            return new ResponseData(wares,true,"获取成功",200);
        }else{
            return new ResponseData(null,false,"获取失败",201);
        }
    }


//    获取指定仓库的具体信息
    @GetMapping("/getOne")
    @Token
    public ResponseData getOne(@RequestHeader String token){

        Admin admin = (Admin)redisTemplate.opsForValue().get("Admin_" + token);
        String wid;
        if(admin.getAuth()==1){
            wid="";
        }else{
            wid=admin.getWid();
        }

        WareInfo wareInfo=wareMapper.getOne(wid);
        QueryWrapper<Place> qw=new QueryWrapper<>();
        qw.eq("wid",wid);
        wareInfo.setPlaceList(placeService.list(qw));

        if (wareInfo!=null){
            return new ResponseData(wareInfo,true,"获取成功",200);
        }else{
            return new ResponseData(wareInfo,false,"获取成功",201);
        }
    }


//针对普通用户
    @GetMapping("/getOneP")
    public ResponseData getOneP(String wid){
        WareInfo wareInfo=wareMapper.getOne(wid);
        QueryWrapper<Place> qw=new QueryWrapper<>();
        qw.eq("wid",wid);
        wareInfo.setPlaceList(placeService.list(qw));

        if (wareInfo!=null){
            return new ResponseData(wareInfo,true,"获取成功",200);
        }else{
            return new ResponseData(wareInfo,false,"获取成功",201);
        }
    }











}
